package cn.edu.nuc.ssm.controller.sys;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.Sysuser;
import cn.edu.nuc.ssm.service.interfaces.SysRoleService;
import cn.edu.nuc.ssm.service.interfaces.SysUserService;

@Controller
@RequestMapping("user")
public class UserController {
    
	@Autowired
	private SysUserService service;
	@Autowired
	private SysRoleService roleservice;
	/**
	 * 进入用户列表(查询)
	 * @return
	 */
	@RequestMapping(value="list",method=RequestMethod.GET)
	public String getUser(@RequestParam(name="username",defaultValue="") String username,
			@RequestParam(name="current",defaultValue="1") int current , 
			Model model){
		Pager<Sysuser> pager = service.userlist(username,current);
		model.addAttribute("pager", pager);
		return "sys/user/list";
	}
	/**
	 * 进入注册用户页面(初始化用户角色),和用户注册功能
	 * @return
	 */
	@RequestMapping(value="add",method=RequestMethod.GET)
	public String adduser(Model model){
		model.addAttribute("rolelist", roleservice.roleListForUser());
		return "sys/user/add";
	}
	@RequestMapping(value="add",method=RequestMethod.POST)
	public String adduser(Sysuser user,Model model){
		   int result = service.regist(user);
		   if(result == 1){
			  return "redirect:list";
		   }
		return "forward:/WEB-INF/views/sys/user/add.jsp";
	}
	/**
	 * 更新，（全部进行更新）
	 */
	//获取用户信息（不能修改密码）
	@RequestMapping(value="detail/{userid}",method=RequestMethod.GET)
	public String updateFun(@PathVariable("userid") Integer userid,Model model){
		Sysuser user = service.selectByUserid(userid);
		model.addAttribute("user", user);
		return "sys/user/update";
	}
	//更新
	@RequestMapping(value="update/{userid}",method=RequestMethod.PUT)
	public String updateFun(@PathVariable("userid") Integer userid,Sysuser user){
		int result = service.updateByPrimaryKey(user);
		if(result == 1){
			return "redirect:/user/list";	
		}
		return  "error";
	}
	/**
	 * 删除单个功能信息
	 */
	@RequestMapping(value="delete/{userid}",method=RequestMethod.DELETE)
	public String deleteFun(@PathVariable("userid") Integer userid){
		int result = service.deleteByPrimaryKey(userid);
		if(result==1){  
			return "redirect:/user/list";	
		}
		return  "error"; 
	}
	
	//修改密码
	
}
